let category
let type
// pages/release/release.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showImg: false,
    name: '',
    desc: '',
    pdesc: '',
    place: '',
    picture: '',
    type: '',
    imgUrl: '',
    select:false,
    tihuoWay:'--选择类型--', 
  },
  bindShowMsg(){
    this.setData({
      select:!this.data.select
    })
  },
  mySelect(e){
    var name = e.currentTarget.dataset.name
    this.setData({
      tihuoWay:name,
      select:false
    })
    category = name
    console.log(name)
  },
  handleChange(e) {
    console.log(e.detail.value)
    if(e.detail.male == 'male'){
      type = '自销'
    }else{
      type = '代销'
    }
  },
  //获取输入的值
  Name(event) {
    console.log(event.detail.value)
    this.setData({
      name: event.detail.value
    })
  },
  Desc(event) {
    console.log(event.detail.value)
    this.setData({
      desc: event.detail.value
    })
  },
  Pdesc(event) {
    console.log(event.detail.value)
    this.setData({
      pdesc: event.detail.value
    })
  },
  Place(event) {
    console.log(event.detail.value)
    this.setData({
      place: event.detail.value
    })
  },
  upfile() {
    // 上传图片第一步：选择你要上传的图片
    wx.chooseImage({
      count: 1, //可以选择多少张图片
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],  //设置图片来源
      success: res=>{
        const filePath = res.tempFilePaths[0]
        console.log(filePath)
        var timestamp = (new Date()).valueOf();
        wx.cloud.uploadFile({ //上传
          cloudPath: 'img/' +timestamp+ '.jpg',
          filePath: filePath,
          success: res => { //全局使用
            console.log('上传成功',res)
            wx.showToast({
              title: '上传图片成功',
            })
            this.setData({
              imgUrl: res.fileID,
            })
          },
          fail(err){
            console.log('上传失败',err)
          }
        })
      }
    })
  },
  
    

  Release() {
    let name = this.data.name
    let desc = this.data.desc
    let place = this.data.place
    let imgUrl = this.data.imgUrl
    let pdesc = this.data.pdesc
    var myDate = new Date();
    // console.log( myDate.toLocaleString( ))
    let time = myDate.toLocaleString( )
    console.log(category)
    wx.cloud.database().collection('product')
    .add({
      data: {
        name: name,
        category: category,
        desc: desc,
        pdesc: pdesc,
        place: place,
        imgUrl: imgUrl,
        type: type,
        time: time
      }
    }).then(res =>{
      console.log('成功发布产品',res)
      wx.showToast({
        title: '成功发布产品',
      })
    }).catch(res =>{
      console.log('发布产品失败',res)
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})